Autoya内からAssetBundleがらみの部分だけを抜き出したモジュールの紹介
概要
これ。
https://github.com/sassembla/Autoya/blob/master/Autoya.AssetBundles.unitypackage
Autoyaはログインから課金まですべからく俺が欲しい機能がいっぱい入っていて、まあ、薄いけどでかい。
そんなにたくさんの機能いらないよ!というのは正直わかる。
なので、まあ、Autoyaは実は複数の機能単位でのモジュールと、それを中央制御するレイヤー(Backyard)でできている。
今回はそれらのモジュールのうち、AssetBundleに関わるものを抜粋したよ、という話。
Autoya AssetBundle Module
ABを生成する部分は含んでいないが、
ABの情報一覧を取得する機構、
AB情報を使って取り出したいAssetを依存関係とか全部みてなんとかする機構、
ABの一覧を更新する機構、
などが入っている。
使い方
1.この世のどこかでABとABListを作ります
2.App内でABListを取得します
3.App内でABに含まれているAssetをロードします
以上! Unloadとかもあるよ。
サンプル
IEnumerator Start()
{
var LIST_DOWNLOAD_URL = "https://raw.githubusercontent.com/sassembla/Autoya/master/AssetBundles/main_assets/OSX/1.0.0/main_assets.json";
var ASSET_NAME = "Assets/AutoyaTests/RuntimeData/AssetBundles/MainResources/textureName.png";
// Download AssetBundle List from web.
var listDownloader = new AssetBundleListDownloader();
AssetBundleList list = null;
var cor = listDownloader.DownloadAssetBundleList(
LIST_DOWNLOAD_URL,
(url, newList) =>
{
list = newList;
},
(code, reason, status) =>
{
Debug.LogError("list download error, code:" + code + " reason:" + reason);
}
);
while (cor.MoveNext())
{
yield return null;
}
// Load Asset from your AssetBundle.
var loader = new AssetBundleLoader(
bundleName =>
{
var path = "https://raw.githubusercontent.com/sassembla/Autoya/master/AssetBundles/" +
list.identity + "/" +
list.target + "/" +
list.version + "/";
return path;
}
);
// set list to AssetBundleLoader.
loader.UpdateAssetBundleList(list);
var loadCor = loader.LoadAsset<Texture2D>(
ASSET_NAME,
(name, tex) =>
{
Debug.Log("asset loaded! tex:" + tex);
},
(name, error, reason, status) =>
{
Debug.LogError("asset load failed. error:" + error + " reason:" + reason);
}
);
while (loadCor.MoveNext())
{
yield return null;
}
}
MonoBehaviourのStartメソッドを使って、
ABListダウンローダーの初期化 ->
ABListのダウンロード ->
ABローダーの初期化 ->
ローダーへのABListのセット ->
ローダーで欲しいAsset名、型を指定して、データをロード
と、こういう感じでできる。
ABListの作り方
これはまだちょっとまどろっこしい。
Autoya+AssetGraphパッケージには、ABListのジェネレータも入っているが、その部分はAssetBundle.unitypackageには入っていない。
なので、次のフォルダをゴソッとgithubからダウンロードして、ABを作成したいプロジェクトに含む必要がある。
https://github.com/sassembla/Autoya/tree/master/Assets/Editor
中にはAssetGraphとAssetBundleListGeneratorのコードが入っている。
この状態でAssetGraphを起動し、適当にABを作るグラフを組み立ててビルドを行うと、プロジェクトフォルダにAssetBundlesフォルダが勝手に作られ、ABが生成され、
自動的にABListが生成される。
一度AssetGraphでビルドしたら、ABListにはバージョンという値が振られる。
バージョン値は、Unity上で Window > Autoya > AssetBundle List Version Editor というので変えられる。
変えた後でAssetGraphのグラフをビルドすると、そのverのABとかABListが出力される。
生成されたABが入っているフォルダを、どこか、、github上とかにでも置くと、物事が解決できるぞ。
やばくなったらCDNとかでググって。